package com.google.code.drift.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.google.code.drift.R;
import com.google.code.drift.dialog.AboutDialog;
import com.google.code.drift.dialog.EnterNickAndSexDialog;
import com.google.code.drift.dialog.QuitRoomDialog;
import com.google.code.drift.dialog.RoomPersonListDialog;
import com.google.code.drift.utils.JsonFactory;
import com.google.code.drift.utils.CommonUtils;

/**
 * @deprecated 这个界面要取消了
 * @author zkwlx
 * 
 */
public class ChatMainActivity extends AbstractActivityGroup implements
		OnClickListener {

	private LinearLayout container = null;

	private TextView tabTraditionMod, tabMapMod;

	private EditText editText;

	private Button sendButton;

	private JsonFactory jsonFactory = JsonFactory.getFactory();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_main_layout);

		initAndSetView();
		onClick(tabTraditionMod);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuItem changeInfo = menu.add(0, 0, 0, R.string.set_user_info);
		changeInfo.setIcon(R.drawable.ic_edit);

		MenuItem roomPerson = menu.add(0, 1, 0, R.string.room_person_list);
		roomPerson.setIcon(R.drawable.ic_room_person);

		MenuItem quitRoom = menu.add(0, 2, 0, R.string.quit_room);
		quitRoom.setIcon(R.drawable.quit_room);

		MenuItem about = menu.add(0, 3, 0, R.string.about);
		about.setIcon(R.drawable.ic_about);

		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new QuitRoomDialog(this).show();
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		quitRoom();
		super.onDestroy();
	}

	@Override
	protected void initActionBar() {
		// TODO Auto-generated method stub

	}

	private static final int ENTER_INFO = 0;

	private static final int ROOM_PERSON_LIST = 1;

	private static final int QUIT_ROOM = 2;

	private static final int ABOUT = 3;

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case ENTER_INFO:
			new EnterNickAndSexDialog(this).show();
			break;
		case ROOM_PERSON_LIST:
			new RoomPersonListDialog(this).show();
			break;
		case QUIT_ROOM:
			new QuitRoomDialog(this).show();
			break;
		case ABOUT:
			new AboutDialog(this).show();
			break;
		}

		return true;
	}

	private Intent tabIntent = null;

	@Override
	public void onClick(View v) {
		setIntentAndButtonState(v.getId());
		startTabActivity(v.getId());
	}

	private void initAndSetView() {
		initView();
		setView();
	}

	private void initView() {
		container = (LinearLayout) findViewById(R.id.activity_container);
		tabTraditionMod = (TextView) findViewById(R.id.tab_tradition_mod);
		tabMapMod = (TextView) findViewById(R.id.tab_map_mod);
		editText = (EditText) findViewById(R.id.edit_text);
		sendButton = (Button) findViewById(R.id.send_button);
	}

	private void setView() {
		tabTraditionMod.setOnClickListener(this);
		tabMapMod.setOnClickListener(this);
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String content = editText.getText().toString();
				if (CommonUtils.isNotNullString(content.trim())) {
					editText.setText(null);
					sendToServerAndSendToMe(content);
				} else {
					showToast(R.string.content_not_null, Toast.LENGTH_SHORT);
				}
			}
		});
	}

	private void startTabActivity(int id) {
		container.removeAllViews();
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		View activityView = getLocalActivityManager().startActivity(
				String.valueOf(id), tabIntent).getDecorView();
		container.addView(activityView, lp);
	}

	private void setIntentAndButtonState(int id) {

		setAllButtonDefault();
		switch (id) {
		case R.id.tab_tradition_mod:
			tabIntent = new Intent(this, ChatTraditionActivity.class);
			setTabButtonState(tabTraditionMod,
					R.drawable.ic_tab_tradition_selected, Color.WHITE);
			break;
		case R.id.tab_map_mod:
			tabIntent = new Intent(this, ChatMapModActivity.class);
			setTabButtonState(tabMapMod, R.drawable.ic_tab_map_selected,
					Color.WHITE);
			break;
		default:
			tabIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			break;
		}
	}

	private void setAllButtonDefault() {
		setTabButtonState(tabTraditionMod, R.drawable.ic_tab_tradition_default,
				Color.GRAY);
		setTabButtonState(tabMapMod, R.drawable.ic_tab_map_default, Color.GRAY);
	}

	private void setTabButtonState(TextView tabButton, int drawId, int textColor) {
		tabButton.setCompoundDrawablesWithIntrinsicBounds(0, drawId, 0, 0);
		tabButton.setTextColor(textColor);
	}

	/**
	 * 给自己发的目的是，刷新自己的聊天View
	 * 
	 * @param content
	 */
	private void sendToServerAndSendToMe(String content) {
		JSONObject json = makeAndSendSayJson(content);
		json = json.getJSONObject("data");
		// 这里是给自己发的广播，所以只要data字段
		// 这个data字段里的内容就是刚刚生成的那个
		CommonUtils.receiveChatDataFromServer(this, json.toJSONString());
	}

	private JSONObject makeAndSendSayJson(String content) {
		JSONObject json = jsonFactory.makeSayContentJson(app.getMyInfo()
				.getUserId(), content, app.getMyInfo().getNick(), app
				.getMyInfo().getSex());
		CommonUtils.sendDataToServer(this, json.toJSONString());
		return json;
	}

	private void quitRoom() {
		makeAndSendQuitRequestJson();
		app.getMyInfo().setRoomId(-1);
	}

	private void makeAndSendQuitRequestJson() {
		JSONObject json = jsonFactory.makeQuitRoomJson(app.getMyInfo()
				.getUserId(), app.getMyInfo().getRoomId());
		CommonUtils.sendDataToServer(this, json.toJSONString());
	}

}
